LIBRARY IEEE;
USE ieee.std_logic_1164.all;
USE ieee.std_logic_arith.all;
USE ieee.std_logic_unsigned.all;
ENTITY pan is
GENERIC(n:INTEGER :=10000000);
port(clk,start,left,right,brake,stop:in std_logic;
	LR:out std_logic_vector(9 downto 0);
	q:out STD_LOGIC); 
	
END ENTITY pan;



ARCHITECTURE one of pan is
TYPE STATETP IS(S1,S2,S3,S4,S5,S6);      --6个状态
SIGNAL regl,regr,regb,regs,regt:STD_LOGIC;
signal counts :integer range n-1 downto 0:=n-1;
signal q1: STD_LOGIC;
BEGIN


process(clk)
 begin
 if rising_edge(clk) then   --如果是时钟的上升沿则
     counts<=counts-1;
     if counts>=n/2 then
       q1<='0';
     else
       q1<='1';
     end if;
     if counts<=0 then
       counts<=n-1;
     end if;
  end if;
q<=q1;
end process;

PROCESS(q1)
variable count:INTEGER RANGE 0 TO 5;
variable num:INTEGER RANGE 0 TO 200:=0;
variable S:STATETP;
variable TAP:std_logic_vector(9 downto 0);
BEGIN


if q1'EVENT AND q1='1' then 
	regl<=left;
	regr<=right;
	regt<=start;
	regb<=brake;                     --上升沿来到检查并且选择状态
	regs<=stop;                    
	if regl='0' AND left='1' then  --左转的键由关到开
		S:=S1;
		TAP:="1000000000";
	elsif regr='0'AND right='1' then --右转的键由关到开
		S:=S2;
		TAP:="0000000001";
	elsif regb='0' AND brake='1' then  --刹车键由关到开
		S:=S3;
		TAP:="1111111111";
	elsif regs='0' AND stop='1' then   --熄火键由关到开
		S:=S4;
		TAP:="0000000000";
	elsif regt='0' AND start='1' then  --启动键由关到开
		S:=S5;
		TAP:="1111111111";
		num:=0;
	elsif regl='1' AND left='0' then  --左转的键由开到关
		S:=S6;
		TAP:="0000000000";
	elsif regr='1'AND right='0' then  --右转的键由开到关
		S:=S6;
		TAP:="0000000000";
	elsif regb='1' AND brake='0' then  --刹车键由开到关
		S:=S6;
		TAP:="0000000000";
	end if;
	count:=count+1;
	if count=5 THEN
		count:=0;
		if S=S1 THEN       --通过拼接实现左转
			TAP(9 DOWNTO 5):=TAP(8 DOWNTO 5)&TAP(9);
		elsif S=S2 THEN    --通过拼接实现右转
			TAP(4 DOWNTO 0):=TAP(0)&TAP(4 DOWNTO 1);
		elsif S=S3 THEN
			TAP:="1111111111";
		elsif S=S4 THEN
			TAP:=TAP;
		elsif S=S5 THEN   --通过计数实现启动后1~2s由亮到灭
			if num<2 then 
				num:=num+1;
				TAP:="1111111111";
			else
				TAP:="0000000000";
			end if;
		elsif S=S6 THEN
			TAP:=TAP;
		end if;
	end if;
end if;
LR<=TAP;
end PROCESS;
end one;
